import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yysk_app/enum/app_enum.dart';
import 'package:yysk_app/model/pay_model/user_list_pay_certification_model.dart';
import 'package:yysk_app/net/api/pay_url.dart';
import 'package:yysk_app/net/http.dart';
import 'package:yysk_app/pages/main/main_page.dart';
import 'package:yysk_app/pages/nft_pages/wallet_pages/wallet/wallet_page.dart';
import 'package:yysk_app/routes/global_controller.dart';
import 'package:yysk_app/utils/colors.dart';
import 'package:yysk_app/utils/dialog_widget.dart';
import 'package:yysk_app/utils/helper.dart';
import 'package:yysk_app/widget/custom_app_bar2.dart';
import 'package:yysk_app/widget/empty_widget.dart';
import 'package:yysk_app/widget/image_asset_widget.dart';
import 'package:yysk_app/widget/lazy_network_image.dart';
import 'manage_bank_page.dart';

class ManageBankTypePage extends StatefulWidget {
  static const path = '/manageBankType';
  const ManageBankTypePage({super.key});
  @override
  State<StatefulWidget> createState() => _ManageBankTypePage();
}

class _ManageBankTypePage extends State<ManageBankTypePage> {
  GlobalController globalController = Get.find();
  final bankList = RxList<UserListPayCertificationModel?>([]);
  @override
  void initState() {
    super.initState();
    globalController.getXPayToken().then((r) {
      getBankList();
    });
  }

  Future getBankList() async {
    await http.netFetch<List<UserListPayCertificationModel>>(
      PayUrl.listPayCertification,
      method: Method.post,
      params: {},
      success: (res) {
        setState(() {
          bankList.value = res.data ?? [];
        });
      },
    );
  }

  void _toManageBank(
    BuildContext context,
    ManageBankType type,
    String channel,
  ) {
    Get.toNamed(
      '${ManageBankPage.path}?type=${type == ManageBankType.personal ? '1' : '2'}&channel=$channel',
      arguments: {
        'type': type == ManageBankType.personal ? '1' : '2',
        'channel': channel,
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    final double top = MediaQuery.of(context).padding.top;
    return Scaffold(
      body: Stack(
        children: [
          //添加背景色，代码如下：
          SizedBox(
            width: context.width,
            child: ImageAssetWidget(
              width: double.infinity,
              height: 173.px,
              imageUrl: 'static/images/wallet/top_bg.png',
            ),
          ),
          Positioned(
            right: 0,
            bottom: 0,
            child: ImageAssetWidget(
              width: 216.px,
              height: 253.px,
              imageUrl: 'static/images/wallet/bottom_bg.png',
            ),
          ),
          Padding(
            padding: EdgeInsets.fromLTRB(0, top, 0, 24.px),
            child: Column(
              children: [
                CustomAppBar2(title: '银行卡管理', context: context),
                Expanded(
                  child: Container(
                    width: double.infinity,
                    decoration: const BoxDecoration(
                      // image: DecorationImage(
                      //   image: AssetImage('static/images/bg_dragon.png'),
                      //   fit: BoxFit.cover,
                      // ),
                    ),
                    child: Obx(
                      () => Column(
                        children: [
                          SizedBox(height: 50.px),
                          if (bankList.isEmpty) EmptyWidget(),
                          ...bankList.map((e) {
                            return _buildEntry(
                              item: e!,
                              logo: 'static/images/mine/ann.png',
                              title:
                                  e.plat == 'USER'
                                      ? '汇付支付（个人版）'
                                      : (e.plat == 'ENTERPRISE'
                                          ? '汇付支付（企业版）'
                                          : '汇付支付（个体工商户）'),
                              onTap: () {
                                //暂时全去绑卡逻辑
                                // _toManageBank(context,e.plat=='USER'? ManageBankType.personal:ManageBankType.enterprise);
                                //下面后续打开
                                if (e.isCertified == true &&
                                    e.certificationStatus == 'PASS') {
                                  _toManageBank(
                                    context,
                                    e.plat == 'USER'
                                        ? ManageBankType.personal
                                        : ManageBankType.enterprise,
                                    e.channel!,
                                  );
                                } else {
                                  toOpenWallet(context: context);
                                }
                              },
                            );
                          }),
                          // _buildEntry(
                          //   logo: 'static/images/mine/ann.png',
                          //   title: '汇付支付（个人版）',
                          //   onTap: () => _toManageBank(context, ManageBankType.personal),
                          // ),
                          // _buildEntry(
                          //   logo: 'static/images/mine/ann.png',
                          //   title: '汇付支付（企业版）',
                          //   onTap: () => _toManageBank(context, ManageBankType.enterprise),
                          // ),
                        ],
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  //去开通钱包
  toOpenWallet({required BuildContext context}) async {
    await showConfirmDialog(
      context: context,
      message: '请先开通钱包',
      messages: [],
      fontSize: 16,
      actions: [
        ActionModel(label: '取消', onPressed: () {}),
        ActionModel(
          label: '前往开通',
          onPressed: () {
            try {
              Get.until((route) => Get.currentRoute == WalletPage.path);
              //  Get.toNamed(WalletPage.path);
            } catch (e) {
              Get.offAllNamed(MainPage.path);
              Get.toNamed(WalletPage.path);
            }
          },
        ),
      ],
    );
  }

  Widget _buildEntry({
    required String logo,
    required String title,
    required VoidCallback onTap,
    required UserListPayCertificationModel item,
  }) {
    return Container(
      margin: EdgeInsets.fromLTRB(10, 0, 19, 21.px),
      // padding: const EdgeInsets.all(16),
      width: double.infinity,
      height: 87.px,
      decoration: BoxDecoration(
        // color: Colors.white.withOpacity(0.8),
        image: const DecorationImage(
          image: AssetImage('static/images/wallet/bank_cell_bg.png'),
          fit: BoxFit.cover,
        ),
        // borderRadius: BorderRadius.circular(20),
        // border: Border.all(color: const Color(0xFFB2E9E2), width: 2),
        // boxShadow: [
        //   BoxShadow(
        //     color: Colors.green.withOpacity(0.1),
        //     blurRadius: 8,
        //     offset: const Offset(0, 4),
        //   ),
        // ],
      ),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          SizedBox(width: 31.px),
          // ImageAssetWidget(
          //   imageUrl: 'static/images/wallet/newpay_logo2.png',
          //   width: 30.px,
          //   height: 30.px,
          // ),
          LazyImage(imageUrl: '${item.iconUrl}', width: 30.px, height: 30.px),
          SizedBox(width: 6.px),
          Expanded(
            child: Text(
              title,
              style: TextStyle(
                color: AppColors.fontColorGreen,
                fontSize: 16.px,
                fontWeight: FontWeight.bold,
                fontFamily: 'CustomFont',
              ),
            ),
          ),
          IconButton(
            icon: Image.asset(
              'static/images/wallet/bank_cell_manager.png',
              width: 88.px,
              height: 29.px,
            ),
            onPressed: onTap,
          ),
          SizedBox(width: 23.px),
        ],
      ),
    );
  }
}
